﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


namespace Common
{
    /// <summary>
    /// Summary description for Logger.
    /// </summary>
    public class Logger
    {
        private static Logger current;
        private StreamWriter file = StreamWriter.Null;
        private string name;
        private StringBuilder sb = new StringBuilder();

        // Constructor privado para evitar instanciaciones
        private Logger()
        {
        }

        public StringBuilder stringBuilder
        {
            get { return sb; }
        }
        // Constructor estatico
        static Logger()
        {
            current = new Logger();
        }

        public static Logger Current
        {
            get { return current; }
        }

        public void Open(string fileName)
        {
            Close();

            try
            {
                name = fileName;
                Logger.Current.Log("Creating log file: " + fileName);
                file = new StreamWriter(fileName);
                file.AutoFlush = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine("No se puede crear el archivo de log: " + ex.Message);
            }
        }

        public void Close()
        {
            if (file != StreamWriter.Null)
            {
                Logger.Current.Log("Closing log file");
                file.Close();
                file = StreamWriter.Null;
            }
        }

        public void Log(string message)
        {
            string sErrInCQ = string.Empty;
            bool bErrInCQ = false;
            DateTime now = DateTime.Now;

            #region Log in CQ

            //			if(p!=null)
            //			{
            //				
            //				try
            //				{
            //					SessionClass cqSession = new SessionClass();
            //
            //					cqSession.UserLogon("pacay","pacay", "CDV06", 2,string.Empty);
            //				
            //					OAdEntity logEntity = (OAdEntity)(cqSession.BuildEntity("Logger"));
            //					logEntity.SetFieldValue("idRelease", p.IdRelease);
            //					logEntity.SetFieldValue("fecha", now.ToLongTimeString());
            //					logEntity.SetFieldValue("msg", message);
            //					logEntity.Validate();
            //					logEntity.Commit();
            //
            //					cqSession.SignOff();
            //				}
            //				catch(Exception ex)
            //				{
            //					sErrInCQ = "Error al tratar de escribir " + message + " en CQ. " + ex.Message;
            //				}
            //			}

            #endregion

            bErrInCQ = (sErrInCQ != string.Empty);

            message = now.ToLongTimeString() + " > " + message;
            //			sErrInCQ = now.ToLongTimeString() + " > " + sErrInCQ; 

            if (file != StreamWriter.Null)
            {
                file.WriteLine(message);
                //				if(bErrInCQ)
                //					file.WriteLine(sErrInCQ);
            }

            Console.WriteLine();
            Console.Write(message);
            sb.AppendLine(message);

            if (bErrInCQ)
                Console.Write(sErrInCQ);
        }

        public string Read2End()
        {
            StreamReader sr;
            bool itWasOpen = false;
            string a = string.Empty;
            if (file != StreamWriter.Null)
            {
                //Si estaba abierto cerrar
                itWasOpen = true;
                file.Close();
            }

            sr = new StreamReader(name);
            a = sr.ReadToEnd();
            sr.Close();

            if (itWasOpen)
            {	//Lo dejo como lo encontre.
                file = new StreamWriter(name);
                file.AutoFlush = true;
            }
            return a;
        }
    }
}
